package com.example.config;

import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

import java.util.Map;

@Configuration
public class EnvironmentConfig2 implements EnvironmentAware {
    @Override
    public void setEnvironment(Environment environment) {
        ConfigurableEnvironment environmentTemp=(ConfigurableEnvironment)environment;
        MutablePropertySources propertySources = environmentTemp.getPropertySources();
        for (PropertySource<?> propertySource : propertySources) {
            if (propertySource instanceof OriginTrackedMapPropertySource){
                OriginTrackedMapPropertySource om=(OriginTrackedMapPropertySource)propertySource;
                Map<String, Object> source = om.getSource();
                source.forEach((k,v)->{
                    String property = environment.getProperty(k);
                    if (property.startsWith("PWD@")){
                        String replace = property.replace("PWD@", "");
                        source.put(k,replace+"thispwd");
                    }
                });
            }
        }
    }
}
